#ifndef DBHANDLER_H_
#define DBHANDLER_H_

/* data type to return an answer to a query */
typedef struct
{
	char ** result;
	int nrow, ncol;
	char * zErrMsg;
} resultT;


/**
 ** makes a query to the database
 **
 ** @param	db		database file name
 ** @param	sql		the sql query
 ** @param	rta		the parameter where it leaves the answer to the query
 ** @return			Returns 0 if all went OK, or an error code if
 **					an error ocurred.
 **/
int query(char * db, char * sql, resultT * rta);

/**
 ** Eliminates some garbage from the DB
 **
 ** @param	database	database file name
 **/
void purgeDB(char * database);

/**
 ** check if user exists and password matchs
 **
 ** @param	database	database file name
 ** @param	pass		password
 ** @param	user		user
 ** @return				Returns the user ID if the user exists, or a negative
 **						value if not
 **/
int checklogin(char * database, char * pass, char * user);

/**
 ** check if the pass from a certain user matches
 **
 ** @param	database	database file name
 ** @param	login		user ID
 ** @param	pass		password
 ** @return				Returns a positive int if password match, or a negative
 **						value if not
 **/
int checkPass(char * database, int login, char * pass);

/**
 ** changes the pass of a user
 **
 ** @param	database	database file name
 ** @param	login		user ID
 ** @param	newpass		new password
 ** @return				Returns a positive int if password match, or a negative
 **						value if not
 **/
int changePass(char * database, int login, char * newpass);

/**
 ** prints in stdout the answer to a query
 **
 ** @param	rta			data type where the answer is kept
 ** @param	start		offset where the print should start
 **/
void printRta(resultT rta, int start);

/**
 ** prints in a string the answer to a query
 **
 ** @param	rta			data type where the answer is kept
 ** @param	start		offset where the print should start
 ** @param	s			pointer to the string where the answer should be printed
 **/
void sprintRta(resultT rta, int start, char ** s);

/**
 ** free the memory allocated in rta
 **
 ** @param	rta			memory to free
 **/
void freeR(resultT rta);

/**
 ** check if the DB file exists
 **
 ** @param	filename	database file name
 ** @return				Returns 1 if exists, 0 if not
 **/
int dbfileExists(char * filename);

/**
 ** check the DB integrity
 **
 ** @param	filename	database file name
 ** @return				Returns 1 if OK, 0 if not
 **/
int checkDB(char * dbName);

/**
 ** check if the file is on the DB
 **
 ** @param	database	database file name
 ** @param	filename	file name
 ** @return				Returns 1 if exists, 0 if not
 **/
int checkFile(char * database, char * filename);

/**
 ** obtain the user ID
 **
 ** @param	database	database file name
 ** @param	user		user
 ** @return				Returns the ID, negative if error
 **/
int checkID(char * database, char * user);

/**
 ** check if the user is downloading a file
 **
 ** @param	database	database file name
 ** @param	login		user ID
 ** @return				Returns 1 if positive, 0 if not
 **/
int downloading(char * database, int login);

/**
 ** counts the current total of downloading connections
 **
 ** @param	database	database file name
 ** @return				Returns the number of downloading connections
 **/
int countConn(char * database);

/**
 ** check the file name asociated to a certain ticket ID
 **
 ** @param	database	database file name
 ** @param	id			ticket ID
 ** @param	name		string where it leaves the answer
 **/
void getFilenameByID(char * database, char * id, char * name);

/**
 ** checks if the user and pass math
 **
 ** @param	database	database file name
 ** @param	pass		password
 ** @param	user		user
 ** @return				Returns the user ID, or negative if error
 **/
int do_checklogin(char * database, char * pass, char * user);

/**
 ** get the pass from a certain user
 **
 ** @param	database	database file name
 ** @param	pass		string where it leaves the answer
 ** @param	user		user
 ** @return				Returns positive int if OK, negative if error
 **/
int GetPass(char * database, char * pass, char * user);

/**
 ** get the categories list
 **
 ** @param	database	database file name
 ** @param	output		here it leaves the answer
 ** @return				Returns the length of the answer if OK,negative if error
 **/
int requestCatList(char * database, char ** output);

/**
 ** get the users list
 **
 ** @param	database	database file name
 ** @param	output		here it leaves the answer
 ** @return				Returns the length of the answer if OK,negative if error
 **/
int requestUsrList(char * database, char ** output);

/**
 ** get the files list from a category
 **
 ** @param	database	database file name
 ** @param	cat			category
 ** @param	output		here it leaves the answer
 ** @return				Returns the length of the answer if OK,negative if error
 **/
int requestFList(char * database, char * cat, char ** output);

/**
 ** logs in the DB a file transfer start
 **
 ** @param	db			database file name
 ** @param	login		user ID
 ** @param	ip			user IP
 ** @param	file		file name been transmited
 ** @param	state		indicates if the transmition its an upload or a download
 **/
void logConn(char * db, int login, char * ip, char * file, short int state);

/**
 ** unlog from the DB a file transfer
 **
 ** @param	db			database file name
 ** @param	login		user ID
 ** @param	ip			user IP
 ** @param	file		file name been transmited
 **/
void unlogConn(char * db, int login, char * ip, char * file);

/**
 ** print in stdout current connections (only file transmissions)
 **
 ** @param	db			database file name
 **/
void printCons(char * db);

/**
 ** adds a file to the DB
 **
 ** @param	database		database file name
 ** @param	filename		file to add name
 ** @param	size			file to add size
 **/
void addFile(char * database, char * filename, int size);

/**
 ** gets user name from DB
 **
 ** @param	database		database file name
 ** @param	login			user ID
 ** @param	user			string pointer where it leaves the answer
 ** @Return 				positive int if all OK, negative if not
 **/
int getUsername(char * database, int login, char ** user);

/**
 ** gets the server space load
 **
 ** @param	database		database file name
 ** @Return					Returns the load of the server in bytes
 **/
int getServerLoad(char * database);

#endif /*DBHANDLER_H_*/

